import java.util.Stack;

/*
* 退格字符
* */

class Solution4 {
    public boolean backspaceCompare(String s, String t) {
        Stack<Character> stack1=new Stack<>();
        Stack<Character> stack2=new Stack<>();
        int len1=s.length();
        int len2=t.length();
        for(int i=0;i<len1;i++){
            char ch=s.charAt(i);
            if(ch=='#'&&!stack1.isEmpty()){
                stack1.pop();
            }else if(ch!='#'){
                stack1.push(ch);
            }
        }
        for(int i=0;i<len2;i++){
            char ch=t.charAt(i);
            if(ch=='#'&&!stack2.isEmpty()){
                stack2.pop();
            }else if(ch!='#'){
                stack2.push(ch);
            }
        }
        while(!stack1.isEmpty()&&!stack2.isEmpty()){
            if(stack1.pop()!=stack2.pop())
            return false;

        }
        return stack1.isEmpty()==stack2.isEmpty();
    }

    public static void main(String[] args) {
        Solution4 solution4=new Solution4();
        solution4.backspaceCompare("y#fo##f","y#f#o##f");
    }
}